package com.flynn.rock.crm.controller;

import com.flynn.rock.annotation.OperateLog;
import com.flynn.rock.crm.model.customer.request.CancelledCustomerOrderRequest;
import com.flynn.rock.crm.model.customer.request.FinishedCustomerOrderRequest;
import com.flynn.rock.crm.model.customer.request.NewInvitationRequest;
import com.flynn.rock.crm.model.customer.request.PageCustomerOrderRequest;
import com.flynn.rock.crm.model.customer.request.SignedCustomerOrderRequest;
import com.flynn.rock.crm.model.customer.request.VisitedCustomerOrderRequest;
import com.flynn.rock.crm.model.customer.vo.CustomerOrderIdVo;
import com.flynn.rock.crm.model.customer.vo.CustomerOrderVo;
import com.flynn.rock.crm.service.customer.BusinessService;
import com.flynn.rock.enums.BusinessType;
import com.flynn.rock.enums.OperateType;
import com.flynn.rock.model.PageResult;
import com.flynn.rock.model.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/crm/business")
@AllArgsConstructor
@Tag(name = "业务管理")
public class BusinessController {

    @Resource
    private BusinessService businessService;

    @OperateLog(operateTitle = "新增邀约", operateType = OperateType.CREATE, businessType = BusinessType.CRM)
    @PostMapping("/order/invitation")
    @Operation(summary = "新增邀约")
    public Response<CustomerOrderIdVo> newInvitationCustomerOrder(@RequestBody @Validated NewInvitationRequest request) {
        return Response.success(businessService.invitationCustomerOrder(request));
    }

    @OperateLog(operateTitle = "确认上门", businessType = BusinessType.CRM)
    @PostMapping("/order/visited")
    @Operation(summary = "确认上门")
    public Response<CustomerOrderIdVo> visitedCustomerOrder(@RequestBody @Validated VisitedCustomerOrderRequest request) {
        return Response.success(businessService.visitedCustomerOrder(request));
    }

    @OperateLog(operateTitle = "客户签单", businessType = BusinessType.CRM)
    @PostMapping("/order/signed")
    @Operation(summary = "客户签单")
    public Response<CustomerOrderIdVo> signedCustomerOrder(@RequestBody @Validated SignedCustomerOrderRequest request) {
        return Response.success(businessService.signedCustomerOrder(request));
    }

    @OperateLog(operateTitle = "商单完成", businessType = BusinessType.CRM)
    @PostMapping("/order/finished")
    @Operation(summary = "商单完成")
    public Response<CustomerOrderIdVo> finishedCustomerOrder(@RequestBody @Validated FinishedCustomerOrderRequest request) {
        return Response.success(businessService.finishedCustomerOrder(request));
    }

    @OperateLog(operateTitle = "废弃商单", businessType = BusinessType.CRM)
    @PostMapping("/order/cancelled")
    @Operation(summary = "废弃商单")
    public Response<CustomerOrderIdVo> cancelledCustomerOrder(@RequestBody @Validated CancelledCustomerOrderRequest request) {
        return Response.success(businessService.cancelledCustomerOrder(request));
    }

    @GetMapping("/order/{customerOrderId}")
    @Operation(summary = "查询客户订单信息")
    public Response<CustomerOrderVo> getCustomerOrder(@PathVariable("customerOrderId") Long customerOrderId) {
        return Response.success(businessService.getCustomerOrder(customerOrderId));
    }

    @OperateLog(operateTitle = "删除邀约", operateType = OperateType.DELETE, businessType = BusinessType.CRM)
    @DeleteMapping("/order/{customerOrderId}")
    @Operation(summary = "删除客户订单信息")
    public Response<Void> deleteCustomerOrder(@PathVariable("customerOrderId") Long customerOrderId) {
        businessService.deleteCustomerOrder(customerOrderId);
        return Response.success(null);
    }

    @PostMapping("/order/page")
    @Operation(summary = "分页查询客户订单")
    public Response<PageResult<CustomerOrderVo>> pageCustomerOrder(@RequestBody PageCustomerOrderRequest request) {
        return Response.success(businessService.pageCustomerOrder(request));
    }
}
